/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.model.sql;

import com.terracloud.server.common.data.log.PlatformLog;
import com.terracloud.server.common.data.log.LogLevel;
import com.terracloud.server.common.data.log.LogType;
import com.terracloud.server.common.data.id.PlatformLogId;
import com.terracloud.server.common.data.id.TenantId;
import com.terracloud.server.common.data.id.UserId;
import com.terracloud.server.dao.model.BaseSqlEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.UUID;

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "platform_log")
public class PlatformLogEntity extends BaseSqlEntity<PlatformLog> {

    @Column(name = "tenant_id")
    private UUID tenantId;
    
    @Column(name = "user_id")
    private UUID userId;
    
    @Column(name = "user_name")
    private String userName;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "log_type")
    private LogType logType;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "log_level")
    private LogLevel logLevel;
    
    @Column(name = "business_id")
    private String businessId;
    
    @Column(name = "operation_content")
    private String operationContent;
    
    @Column(name = "operation_details")
    private String operationDetails;
    
    @Column(name = "operation_params")
    private String operationParams;
    
    @Column(name = "status_transition")
    private String statusTransition;
    
    @Column(name = "exception_info")
    private String exceptionInfo;
    
    @Column(name = "occur_time")
    private Long occurTime;
    
    @Column(name = "ip_address")
    private String ipAddress;
    
    @Column(name = "status")
    private String status;

    public PlatformLogEntity() {
        super();
    }

    public PlatformLogEntity(PlatformLog platformLog) {
        if (platformLog.getId() != null) {
            this.setId(platformLog.getId().getId());
        }
        if (platformLog.getTenantId() != null) {
            this.tenantId = platformLog.getTenantId().getId();
        }
        if (platformLog.getUserId() != null) {
            this.userId = platformLog.getUserId().getId();
        }
        this.userName = platformLog.getUserName();
        this.logType = platformLog.getLogType();
        this.logLevel = platformLog.getLogLevel();
        this.businessId = platformLog.getBusinessId();
        this.operationContent = platformLog.getOperationContent();
        this.operationDetails = platformLog.getOperationDetails();
        this.operationParams = platformLog.getOperationParams();
        this.statusTransition = platformLog.getStatusTransition();
        this.exceptionInfo = platformLog.getExceptionInfo();
        this.occurTime = platformLog.getOccurTime();
        this.ipAddress = platformLog.getIpAddress();
        this.status = platformLog.getStatus();
    }

    @Override
    public PlatformLog toData() {
        PlatformLog platformLog = new PlatformLog(new PlatformLogId(this.getId()));
        platformLog.setCreatedTime(this.getCreatedTime());
        if (this.tenantId != null) {
            platformLog.setTenantId(new TenantId(this.tenantId));
        }
        if (this.userId != null) {
            platformLog.setUserId(new UserId(this.userId));
        }
        platformLog.setUserName(this.userName);
        platformLog.setLogType(this.logType);
        platformLog.setLogLevel(this.logLevel);
        platformLog.setBusinessId(this.businessId);
        platformLog.setOperationContent(this.operationContent);
        platformLog.setOperationDetails(this.operationDetails);
        platformLog.setOperationParams(this.operationParams);
        platformLog.setStatusTransition(this.statusTransition);
        platformLog.setExceptionInfo(this.exceptionInfo);
        platformLog.setOccurTime(this.occurTime);
        platformLog.setIpAddress(this.ipAddress);
        platformLog.setStatus(this.status);
        return platformLog;
    }
}